import { post } from '@/utils/request'

export const fetchSystemConfig = async () => {
  localStorage.setItem('systemConfig', '{}')
  const systemConfig = {}
  const { data, code } = await post('/operation/opex/getSettingInfoLists', {})
  if (code === 0) {
    data.forEach((item) => {
      systemConfig[item.sname] = item
    })
    localStorage.setItem('systemConfig', JSON.stringify(systemConfig))
  }
}
export const fetchConfigByName = async (zjm) => {
  const { data, code } = await post('/operation/opex/getSettingInfoByName', { zjm })
  return code === 0 ? data.svalue : ''
}
export const getSystemConfig = (code) => {
  const systemConfig = localStorage.getItem('systemConfig')
  if (systemConfig) {
    const config = JSON.parse(systemConfig)[code]
    return config?.svalue || ''
  } else {
    fetchSystemConfig()
    // 使用标志位防止重复刷新
    const refreshFlag = sessionStorage.getItem('systemConfigRefreshing')
    if (!refreshFlag) {
      sessionStorage.setItem('systemConfigRefreshing', 'true')
      setTimeout(() => {
        sessionStorage.removeItem('systemConfigRefreshing')
        location.reload()
      }, 500)
    }
  }
}
